﻿using System.Web;
using System.Web.SessionState;


//using iSun.CRM.Library;

using iSun.Model;
using iSun.BLL.Base;

namespace iSun.Web.UI.Controls
{
    public class DataTableJsonData : JsonData, IHttpHandler, IRequiresSessionState 
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string strEntity = this.GetStringFromQueryString("entity", "DatUser");            //对应的实体
            string strCondition = this.GetStringFromQueryString("condition", "");            //系统特定条件
            string strCustomCondition = this.GetStringFromQueryString("customCondition", "");//自定义条件
            int iPageIndex = this.GetIntFromQueryString("pageindex", 0);                     //第N页？
            int iPageSize = this.GetIntFromQueryString("pagesize", 0);                       //页记录数？
            string IsOnlyTotal = this.GetStringFromQueryString("onlyTotal", "false");         //是否只回传记录总数?
            string IsDesc = this.GetStringFromQueryString("desc", "false");                  //Yes or No

            string strType = this.GetStringFromQueryString("type", "");
            string strOrder = this.GetStringFromQueryString("order", "");
            strEntity = strEntity.Split(',')[0];

            BaseEntity entity = iSun.Common.Reflection.CreateEntitysInstance(strEntity) as BaseEntity;
            if (entity == null)
            {
                entity = new DataTableBaseEntity();
                var _entity = entity as DataTableBaseEntity;
                //_entity.TableKey = _entity.GetTableId();
                _entity.TableName = strEntity;
            }
            else
            {
                string tableName = entity.GetTableName();
                entity = new DataTableBaseEntity();
                var _entity = entity as DataTableBaseEntity;
                //_entity.TableKey = _entity.GetTableId();
                _entity.TableName = tableName;
            }
            BaseBussnies<BaseEntity> bll = new BaseBussnies<BaseEntity>(entity);
            //System.Data.DataTable = bll.GetDataBySqlComm();
            bll.PageIndex = iPageIndex;
            bll.PageSize = iPageSize == 0 ? 25 : iPageSize;
            bll.Order = strOrder;
            bll.Desc = "Yes";
            System.Data.DataTable list;

            int totalrecord = bll.GetRowCount(strCondition);
            if (IsOnlyTotal == "true")
            {
              context.Response.Write("[{\"total\":" + totalrecord.ToString() + "}]");
                return;
            }
            if (bll.PageIndex > 0)
            {
                list = string.IsNullOrEmpty(strCondition) ? bll.GetDataTableSplitPage("1=1") : bll.GetDataTableSplitPage(strCondition);
            }
            else
            {
                list = string.IsNullOrEmpty(strCondition) ? bll.GetDataTableByCondition("1=1") : bll.GetDataTableByCondition(strCondition);
            }
            if (list == null)
            {
                context.Response.Write("[{\"N\":\"null\"}]");
                return;
            }
            //int totalrecord = bll.GetRowCount(strCondition);
            string strFormat = "{\"total\":" + totalrecord.ToString() + ",\"items\":";
            string str = iSun.Common.Globals.DataTableToJSON(list).Replace("\n", " ").Replace("\r", " ").Replace("null", "").Replace("[BR]", " ").Replace("@@@@", " ").Replace("@@", " ");
            strFormat = strFormat + (str == "" ? "0" : str) + "}";
            //strFormat = strFormat.Replace(new Regex("(^|[^\\\\])\\\"\\\\/Date\\((-?[0-9]+)\\)\\\\/\\\"","$1new Date($2)");
            if (strType == "1")
            {
                context.Response.Write("[" + str.Replace("null", "").Replace("[A]", "&") + "]");
            }
            else
            {
                context.Response.Write(strFormat);
            }
            //iSunContext.Current.Context.Response.Write("["+str+"]");
        }

        #endregion
    }
}
